# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'
__all__ = []

from ... import autostacks
from ... import comparecards as cmp_cards
from ... import formlib as layout
from ... import pilelib as stack
from ...packlib import info as cs
from ...gamelib import info as gi, game

GT, SL = gi.GT, gi.SL
Wrap = stack.StackWrapper


class FlowerClock(cs.Hanafuda_Deck, game.Game):
    """
    Finish cards on _foundation_ within 1 turn.

    Layout
      - row [8 over 2 rows, vert offset]
      - talon [SW, no offset]
      - foundation [12 circle E, no offset]

    :CVariables:
      _talon_ : pysollib.stack.Stack
        .. stack.InitialDealTalon

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation (base_rank=cs.FOURTH, dir=-1, max_cards=4)

        Build down in rank by same suit wrap. Play top. Piles build with any
        base suit but will only finish with suit of pile label.

      _row_ : pysollib.stack.Stack
        .. stack.RK_RowStack (max_move=1, max_accept=1)

        Init 5 card. Build up in rank alone. Play top.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.defaultHint

    :note:
      ⟪autodrop⟫ disabled for mouse events.

    :todo: needs seperate layout
    """
    __info__ = dict(id=0x92c, name='Flower Clock', category=cs.TYPE_HANAFUDA,
                    game_type=(GT.ONE_DECK | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='3.20')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _talon_ = stack.InitialDealTalon
    _foundation_ = Wrap(stack.SS_Foundation, base_rank=cs.FOURTH, dir=-1,
                        max_cards=4)
    _row_ = Wrap(stack.RK_RowStack, base_rank=cs.FIRST, max_cards=8)

    def _create_game(self, rows=8, playcards=8, **kw):
        l, s = layout.Layout(self), self.s
        dy = max(3 * l.YS / 2 + l.CH, l._get_stack_height(playcards))
        self.table_size = ((rows / 2 + 5) * l.XS + l.XM, l.YM + 2 * dy,)
        for x, y in l.col_block_iter(l.XM, l.YM, npiles=rows, ncols=rows / 2,
                                     ycards=playcards):
            s['rows'].append(self._row_(x, y, game=self))
        dx, dy = x - l.XM / 2, l.YM + dy - l.HCH
        self._set_region(s['rows'][:4], self._region(w=dx, h=dy))
        self._set_region(s['rows'][4:], self._region(y=dy, w=dx))
        piles = [(x, y,) for x, y in l.round_iter(12, rows / 2 * l.XS + l.XM)]
        for (x, y,), suit in zip(piles, range(cs.PINE, cs.PAULOWNIA + 1)):
            s['foundations'].append(self._foundation_(x, y, game=self,
                                                   suit=cs.ANY))
            l.set_misc_text(s['foundation'], anchor='s', text=self.SUITS[suit])
        s['talon'] = self._talon_(self.width - l.XS, self.height - l.YS,
                               game=self)
        l.defaultStackGroups()
        return l

    def _start(self, rows=5, flip=True, **kw):
        """
        Deal initial cards for new game.
          - s[rows] 5 ea up.

        .. \_start([rows=5, flip=True]) -> None
        """
        for i in range(rows - 1):
            self.s['talon'].dealRow(flip=flip, frames=0)
        self.s['talon'].dealRow()

    @property
    def is_complete(self):
        """
        Check `s.foundations` `is_filled` and are in suit order from 1:00.
        """
        return all(s.is_filled and
                   cmp_cards.isSameSuit(s.cards, suit=s.BASECARD['suit'])
                   for s in self.s['foundations'])

    def _autopiles(self, event=None):
        """
        Return piles for auto or quick move.

        Overide to disable autodrop when not event.

        .. \_autopiles(self, event=None) -> seq(seq(Pile ...), seq(Pile ...),
             seq(Pile ...))
        """
        piles = super(FlowerClock, self)._autopiles(event)
        return autostacks.flip_quickplay(piles) if event is None else piles


PROVIDES = [FlowerClock]